

class PlanController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ planInstanceList: Plan.list( params ), planInstanceTotal: Plan.count() ]
    }

    def create = {
        //TODO:폼 모양 바꾸기, 중요도:하
        def planInstance = new Plan()
        planInstance.properties = params
        return ['planInstance':planInstance]
    }

    def save = {
        if (!session.user){
            redirect(controller:'user', action:'login')
        }else{
            User user = User.find(session.user)
            def planInstance = new Plan(params)
            if(!planInstance.hasErrors() && planInstance.save()) {
                user.addToPlans(planInstance)
                redirect(controller:'dashboard', action:'planList')
            }
            else {
                render(view:'create',model:[planInstance:planInstance])
            }
        }
    }

//수정은 지원하지 않음
/*
    def show = {
        def planInstance = Plan.get( params.id )

        if(!planInstance) {
            flash.message = "Plan not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ planInstance : planInstance ] }
    }

    def delete = {
        def planInstance = Plan.get( params.id )
        if(planInstance) {
            try {
                planInstance.delete()
                flash.message = "Plan ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Plan ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Plan not found with id ${params.id}"
            redirect(action:list)
        }
    }


    def edit = {

        def planInstance = Plan.get( params.id )

        if(!planInstance) {
            flash.message = "Plan not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ planInstance : planInstance ]
        }
    }

    def update = {
        def planInstance = Plan.get( params.id )
        if(planInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(planInstance.version > version) {

                    planInstance.errors.rejectValue("version", "plan.optimistic.locking.failure", "Another user has updated this Plan while you were editing.")
                    render(view:'edit',model:[planInstance:planInstance])
                    return
                }
            }
            planInstance.properties = params
            if(!planInstance.hasErrors() && planInstance.save()) {
                flash.message = "Plan ${params.id} updated"
                redirect(action:show,id:planInstance.id)
            }
            else {
                render(view:'edit',model:[planInstance:planInstance])
            }
        }
        else {
            flash.message = "Plan not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }
*/
}
